package com.fdb.efp.esb.service.impl.dxmszd;

import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.fdb.efp.esb.common.constant.MsLoanConstant;
import com.fdb.efp.esb.service.facade.dxmszd.OnlineVerificationService;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.basic.framework.core.util.SpringContextUtils;
//import com.fdb.basic.framework.core.util.TraceUtil;
import com.fdb.efp.esb.service.bo.req.dxmszd.OnlineVerificationReq;
import com.fdb.efp.esb.service.bo.resp.dxmszd.OnlineVerificationResp;
import com.fdb.efp.esb.service.client.ESBClientFactory;
import com.fdb.efp.esb.service.client.impl.MSESBBeanCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 联网核查接口
 *
 * @author liangcongming
 * @version 1.0
 * @since 2020年5月8日 下午3:10:08
 */
@Service("onlineVerificationService")
public class OnlineVerificationServiceImpl implements OnlineVerificationService {

    private final static Logger log = LoggerFactory.getLogger(OnlineVerificationServiceImpl.class);

    private static final String LOGGER_ID = "联网核查接口";

    @Override
    public OnlineVerificationResp callOnlineCheck(OnlineVerificationReq onlineVerificationReq) throws Exception {
        OnlineVerificationResp resp = new OnlineVerificationResp();
        EsbRespSysHeadBean esbRespSysHeadBean = new EsbRespSysHeadBean();

        EsbRespServiceBean esbRespServiceBean = new EsbRespServiceBean();
//      【问题背景】马上交易等待时，联网核查任务共用了线程，导致全局流水号重复。
//      【分析】全局流水号对联网核查没影响，清空反而有可能影响当前线程的所执行的任务，故不清空。
//      TraceUtil.clear();

        try {
            esbRespServiceBean = ESBClientFactory.buildClient()
                    .withBody(onlineVerificationReq)
                    .withTradeNo(MsLoanConstant.TRADE_NO_40120001)
                    .withScene(MsLoanConstant.SCENE_01)
                    .withESBBeanFactory((MSESBBeanCreator) SpringContextUtils.getBean("MSESBBeanCreator"))
                    .create().sendAndReceive();
            esbRespSysHeadBean = esbRespServiceBean.getSysHead();

            if (MsLoanConstant.RetCodeEnum.SUCCESS.VALUE.equals(esbRespSysHeadBean.getRetInfArry().get(0).getRetCd())) { // 【1】成功
                resp = esbRespServiceBean.getBody(OnlineVerificationResp.class);
                resp.setResult(MsLoanConstant.RetCodeEnum.SUCCESS.VALUE);
                log.info(LOGGER_ID + ":联网核查接口调用成功，开始进行后处理 ，请求参数： " + onlineVerificationReq.toString() + ", 返回结果： " + resp.toString());
            } else if (esbRespSysHeadBean.getRetInfArry().get(0).getRetCd().startsWith("500")){ // 【2】esb报500，则跳过此次核查，若空指针，捕获之后同样返回null
                log.info(LOGGER_ID + ":联网核查接口调用失败，ESB返回信息：" + esbRespSysHeadBean.getRetInfArry().get(0).getRetMsg());
                return null ;
            } else { // 【3】esb报非500错误，则无需再次核查
                log.info(LOGGER_ID + ":联网核查接口调用失败，ESB返回信息：" + esbRespSysHeadBean.getRetInfArry().get(0).getRetMsg());
                resp.setExmnRslt(esbRespSysHeadBean.getRetInfArry().get(0).getRetMsg());
                resp.setResult(esbRespSysHeadBean.getRetInfArry().get(0).getRetCd());
            }
            return resp;
        } catch (ESBException e1){
            log.error(LOGGER_ID +":联网核查ESB异常！待下次批次轮训时重试！");
            return null ;
        } catch (Exception e) {
            log.error(LOGGER_ID + ":联网核查消费接口调用失败！" + e.getMessage(),e);
        }
        return null;
    }


}
